package com.mall.mall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.mall.common.utils.R;
import com.mall.mall.ware.feign.MemberFeignService;
import com.mall.mall.ware.vo.FareVO;
import com.mall.mall.ware.vo.MemberAddressVO;
import com.sun.xml.internal.ws.util.xml.CDATA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.mall.ware.dao.WareInfoDao;
import com.mall.mall.ware.entity.WareInfoEntity;
import com.mall.mall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignServicel;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> warpper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            warpper.and(w->{
                w.eq("id", key).or().like("name", key).or().like("address", key).or().like("areacode", key);
            });
        }

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                warpper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVO getFare(Long addrId) {
        R info = memberFeignServicel.info(addrId);
        FareVO fareVO = new FareVO();
        MemberAddressVO addressVO = info.getData("memberReceiveAddress", new TypeReference<MemberAddressVO>() {});
        fareVO.setMemberAddressVO(addressVO);
        if (null != addressVO){
            String phone = addressVO.getPhone();
            String substring = phone.substring(phone.length()-1, phone.length());
            BigDecimal fare = new BigDecimal(substring);
            fareVO.setFare(fare);
        }

        return fareVO;
    }

}